<?php
/**
 *  权限表管理
*/

namespace app\admin\service;

use app\facade\Basic;
use app\admin\model\Auth as AuthModel;

class Auth extends BaseService
{


    /**
     *  权限列表
    */
    public function list($param){

        return AuthModel::field('id,module,authname,route,remark,parent_id,create_time,update_time')->paginate();
    }

    /**
     *  权限详情
     */
    public function detail($param)
    {

        $result = AuthModel::where('id',$param['id'])->find();
        if($result){
            return Basic::data(1,'ok',$result);
        } else {
            return Basic::data(0,'路由不存在');
        }
    }

    /**
     *  添加编辑权限
     */
    public function save($param)
    {

        $insData = [];
        $insData['id'] = $param['id'];
        $insData['module'] = $param['module'];
        $insData['authname'] = $param['authname'];
        $insData['route'] = $param['route'];
        $insData['remark'] = $param['remark'];
        $insData['parent_id'] = $param['parent_id'];


        if (empty($insData['id'])) {
            /* 验证权限是否已存在 */
            if (AuthModel::whereOr([
                'authname' => $insData['authname'],
               'route' => $insData['module'],
            ])->find()){
                return Basic::data(1,'权限已存在');
            }

            /* 添加权限 */
            $insData['create_time'] = date('Y-m-d H:i:s');
            $insData['creator'] = $param['operator_id'];   // 管理员ID
            unset($insData['id']);

            $result = AuthModel::create($insData);

            if($result){
                $this->logger->logOperation($result->id,'auth','添加','添加权限');
                return Basic::data(1,'保存成功');
            } else {
                return Basic::data(0,'保存失败');
            }
        } else {

            /* 修改权限 */
            $insData['updater'] = $param['operator_id'];   // 管理员ID
            $insData['update_time'] = date('Y-m-d H:i:s');
            $result = AuthModel::update($insData);
            if($result){
                $this->logger->logOperation($result->id,'auth','修改','修改权限');
                return Basic::data(1,'修改成功');
            } else {
                return Basic::data(0,'修改失败');
            }
        }
    }


    /**
     *  删除角色 ( 软删除)
    */
    public function remove($param)
    {

         $result = AuthModel::where('id',$param['id'])->find();


         if (!$result) {
             return Basic::data(1,'用户不存在');
         }

         if($result->is_del != 0){
             return Basic::data(1,'已经被删除');
         }

        $result->is_del = time();
        $result->update_time = date("Y-m-d H:i:s");

        if($result->save()){
            $this->logger->logOperation($result->id,'auth','删除','删除权限');
            return Basic::data(0,'删除成功');
        } else {
            return Basic::data(1,'删除失败');
        }
    }

    /**
     *  删除角色 ( 物理删除)
    */
    public function del($param)
    {

        $result = AuthModel::where('id',$param['id'])->find();

        if (!$result) {
            return Basic::data(0,'不存在');
        }

        if($result->delete()){
            $this->logger->logOperation($result->id,'auth','删除','删除权限');
            return Basic::data(1,'删除成功');
        } else {
            return Basic::data(0,'删除失败');
        }
    }






}
